//
//  XJ_QuickRepaymentViewController.m
//  XunJieProject
//
//  Created by 薛子 on 2018/6/12.
//  Copyright © 2018年 Soowin. All rights reserved.
//

#import "XJ_QuickRepaymentViewController.h"
#import "XJ_UnsubmissionPlanListViewController.h"
@interface XJ_QuickRepaymentViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIButton *generationPlanBtn;

@property (weak, nonatomic) IBOutlet UILabel *leiXingContent;
@property (weak, nonatomic) IBOutlet UITextField *huanKuanZongErTF;
@property (weak, nonatomic) IBOutlet UITextField *qiDongJinTF;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *backViewTop;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *height_contentView;

@end

@implementation XJ_QuickRepaymentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.nav_titleLabel.text = @"快速还款";
    
    if (IPHONEX) {
        
        _backViewTop.constant = 20;
    }
    
    _height_contentView.constant =SCREEN_HEIGHT;
    _generationPlanBtn.layer.cornerRadius = 5;
    _huanKuanZongErTF.delegate = self;
    _qiDongJinTF.delegate = self;
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)generationPlanAction:(id)sender {
    
    if (_huanKuanZongErTF.text.length == 0) {
        
        ZDXAlertView *alext =[[ZDXAlertView alloc]initWithMessage:@"请输入还款总金额" image:@"pop_msg_tip_xs"];
        [alext showXLAlertView];
        return;
    }
    if (_qiDongJinTF.text.length == 0) {
        
        ZDXAlertView *alext =[[ZDXAlertView alloc]initWithMessage:@"请输入启动金额" image:@"pop_msg_tip_xs"];
        [alext showXLAlertView];
        
        return;
    }
    
    
    if ([_huanKuanZongErTF.text integerValue] < 200) {
        
        ZDXAlertView *alext =[[ZDXAlertView alloc]initWithMessage:@"还款总额为不能小于200元" image:@"pop_msg_tip_xs"];
        [alext showXLAlertView];

        return;
    }
    
    if ([_qiDongJinTF.text integerValue] < 200) {
        
        ZDXAlertView *alext =[[ZDXAlertView alloc]initWithMessage:@"启动金额为不能小于200元" image:@"pop_msg_tip_xs"];
        [alext showXLAlertView];
    
        return;
    }
    
    NSInteger allNum = [_huanKuanZongErTF.text integerValue] + [_qiDongJinTF.text integerValue];
    if (allNum < 200) {
        
        ZDXAlertView *alext =[[ZDXAlertView alloc]initWithMessage:@"还款总额 与 启动金额 必须>=200元" image:@"pop_msg_error_xs"];
        [alext showXLAlertView];
 
        return;
    }
    
    NSUserDefaults * db = [NSUserDefaults standardUserDefaults];
    NSString * token = [db objectForKey:@"token"];
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setValue:IDENTIFIER forKey:@"equipmentNo"];
    [dic setValue:APPVERSION forKey:@"appVersion"];
    [dic setValue:STSTERMVERSION forKey:@"systemVersion"];
    [dic setValue:USERID forKey:@"userId"];
    
    if (token!=nil &&![token isEqualToString:@""]) {
        
        [dic setValue:token forKey:@"token"];
        
    }
    [dic setValue:_cardModel.cardId forKey:@"cardId"];
    [dic setValue:_huanKuanZongErTF.text forKey:@"backTotalMoney"];
    [dic setValue:_qiDongJinTF.text forKey:@"startMoney"];
    
    
    [XJ_Utils signPost:@"plan/add?" parameters:dic success:^(NSInteger recode, id jsonObject) {
        
        NSDictionary * dataDic = [jsonObject objectForKey:@"data"];
        
        NSString * planID = [dataDic objectForKey:@"planId"];
        
        XJ_UnsubmissionPlanListViewController * planListVC = [[XJ_UnsubmissionPlanListViewController alloc]init];
        planListVC.planID = planID;
        [self.navigationController pushViewController:planListVC animated:YES];
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
    
   
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSMutableString * futureString = [NSMutableString stringWithString:textField.text];
    [futureString  insertString:string atIndex:range.location];
    NSInteger flag=0;
    const NSInteger limited = 2;//小数点后需要限制的个数
    for (int i = futureString.length-1; i>=0; i--) {
        
        if ([futureString characterAtIndex:i] == '.') {
            if (flag > limited) {
                return NO;
            }
            break;
        }
        flag++;
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
